package com.nunbey.lingxi.activities;



import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.nunbey.lingxi.R;
import com.nunbey.lingxi.activities.sub.SubBaseActivity;
import com.nunbey.lingxi.lincat.activityinit.ActivityCollector;
import com.nunbey.lingxi.module.list.DoctorRecycleView;
import com.nunbey.lingxi.module.list.GoodRecycleView;
import com.nunbey.lingxi.module.mode.doctors.DoctorInfo;
import com.nunbey.lingxi.module.mode.goods.Good;

import java.time.Duration;
import java.time.LocalTime;
import java.util.ArrayList;

public class IndexActivity extends SubBaseActivity {

    private LocalTime backTime;
    private Chronometer chronometer;
    @Override
    public void onCreate(Bundle savedInstancedState) {
        super.onCreate(savedInstancedState);
        this.backTime = LocalTime.now();
        this.setContentView(R.layout.activity_index);
        webViewInit();
        this.setDoctorsData();

    }

    public static void startMainActivity(Context context){
        context.startActivity(new Intent(context, IndexActivity.class));
    }

    /**
     * @description 用户返回监听
     */
    @Override
    public void onBackPressed() {
        this.backFaultTolerant();
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.rotationImageInit();
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.chronometer.stop();
    }


    /**
     * @name 用户返回容错
     * @description 当用户不小心点下back或home键时触发提示
     * @author 李一帆
     */
    private void backFaultTolerant(){
        LocalTime current = LocalTime.now();
        Duration duration = Duration.between(backTime,current);
        long durationMillis = duration.toMillis();
        backTime = current;
        if(durationMillis > 3*1000)
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_LONG).show();
        else {
            super.onBackPressed();
            ActivityCollector.getActivityCollector().killProcess();
        }
    }

    /**
     * 设置首页医生列表数据
     */
    private void setDoctorsData(){
        ArrayList<DoctorInfo> doctorInfos = new ArrayList<>();
        DoctorInfo doctorInfo1 = new DoctorInfo("程斌","济南市中西医结合医院-骨科","擅长颈椎病，腰椎间盘突出，股骨头坏死，骨性关节炎，肌腱肌肉损伤，多发骨折，断肢再植等骨科常见疾病。","https://img.99.com.cn/uploads/doctor/201902/19021502585533.jpg");
        DoctorInfo doctorInfo2 = new DoctorInfo("刘跃梅","赣南医学院第一附属医院-小儿内科","擅长呼吸系统的疾病诊治，中西医结合治疗儿科疾病。","https://img.99.com.cn/uploads/doctor/201904/19041202350619.jpg");
        DoctorInfo doctorInfo3 = new DoctorInfo("陈波","厦门大学附属中山医院-呼吸内科","擅长治疗肺癌，哮喘，慢性阻塞性肺疾病，睡眠呼吸暂停低通气综合征等症。","https://img.99.com.cn/uploads/doctor/201903/19031205213381.jpg");
        DoctorInfo doctorInfo4 = new DoctorInfo("陈伟","赣南医学院第一附属医院-康复医学科","擅长康复技术与针灸结合治疗脑卒中、颅脑外伤、脊髓损伤、周围神经损伤等神经系统疾病 。","https://img.99.com.cn/uploads/doctor/201904/19041202512323.jpg");
        DoctorInfo doctorInfo5 = new DoctorInfo("艾立坤","首都医科大学附属北京同仁医院-眼科","擅长：各种儿童眼病，儿童及成人斜视、弱视，视疲劳引起的复视，外伤引起的麻痹性斜视及各种特殊类型斜视、肉毒素斜视治疗。","https://img.99.com.cn/uploads/202203/2022030704003468.jpg");
        doctorInfos.add(doctorInfo1);
        doctorInfos.add(doctorInfo2);
        doctorInfos.add(doctorInfo3);
        doctorInfos.add(doctorInfo4);
        doctorInfos.add(doctorInfo5);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.activity_index_doctors);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        DoctorRecycleView.DoctorRecycleViewAdapter adapter = new DoctorRecycleView.DoctorRecycleViewAdapter(doctorInfos,this);
        recyclerView.setAdapter(adapter);
    }

    /**
     * @description 启动淘宝复制商品链接
     * @param view
     */
    public void startTaoBao(View view){
        String goodUri = "https://m.tb.cn/h.fIoDiHU?sm=8b3e2b?tk=BjCj2835CcN";
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setData(Uri.parse(goodUri));
        startActivity (intent);

    }


    /**
     * 轮播图
     */
    private void rotationImageInit(){
        ImageView imageView = findViewById(R.id.activity_index_rotationImage);
        chronometer = findViewById(R.id.activity_index_clock);
        chronometer.setBase(SystemClock.elapsedRealtime());
        chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
            int count = 0;
            @Override
            public void onChronometerTick(Chronometer chron) {
                if(((SystemClock.elapsedRealtime() - chron.getBase()) / 1000) % 4 == 0){
                    if(count==0)    imageView.setImageResource(R.drawable.rotation_image1);
                    else if(count==1) imageView.setImageResource(R.drawable.rotation_image2);
                    else {
                        imageView.setImageResource(R.drawable.rotation_image3);
                    }
                    count++;
                    if(count==3) count=0;
                }
            }
        });
        chronometer.start();
    }

    /**
     * @description 按下返回首页
     * @param view
     */
    public void navigateToIndex(View view){
        String currentActivityClass = this.getCurrentActivityClassString();
        if(!currentActivityClass.equals("com.nunbey.lingxi.activities.IndexActivity")){
            //findViewById(R.id.activity_index_navigateToIndex).setBackgroundResource(R.drawable.index_unclicked);
            //findViewById(R.id.activity_index_navigateToSearch).setBackgroundResource(R.drawable.view_clicked);
            //(R.id.activity_index_navigateToMy).setBackgroundResource(R.drawable.my_clicked);
            IndexActivity.startMainActivity(this);
        }
    }

    /**
     * @description 按下进入搜索页
     * @param view
     */
    public void navigateToSearch(View view){
        String currentActivityClass = this.getCurrentActivityClassString();
        if(!currentActivityClass.equals("com.nunbey.lingxi.activities.SearchActivity")){
            //findViewById(R.id.activity_index_navigateToIndex).setBackgroundResource(R.drawable.index_clicked);
            //findViewById(R.id.activity_index_navigateToSearch).setBackgroundResource(R.drawable.view_unclicked);
            SearchActivity.startSearchActivity(this);
        }
    }

    /**
     * @description 按下进入我的页面
     * @param view
     */
    public void navigateToMy(View view){
        String currentActivityClass = this.getCurrentActivityClassString();
        if(!currentActivityClass.equals("com.nunbey.lingxi.activities.MyActivity")){
            //findViewById(R.id.activity_index_navigateToMy).setBackgroundResource(R.drawable.my_clicked);
            MyActivity.startMyActivity(this);
        }
    }

    /**
     * 跳转到心电页面
     * @param view
     */
    public  void navigateToXinDian(View view){
        //EcgActivity.startEcgActivity(this);
        EcgActivity.startEcgActivity(this);
    }

    /**
     * 首页引入webview初始化
     */
    private void webViewInit(){
        WebView webView = this.findViewById(R.id.activity_index_ai);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("https://abcyun.cn/");
        WebView webView2 = this.findViewById(R.id.activity_index_medician);
        webView2.getSettings().setJavaScriptEnabled(true);
        webView2.getSettings().setDomStorageEnabled(true);
        webView2.setWebViewClient(new WebViewClient());
        webView2.loadUrl("https://m.baiji.com.cn/");
    }


    public void navigateToHospitalRegister(View view) {
        HospitalRegistrationActivity.startHospitalRegistrationActivity(this);
    }

    public void navigateToXinGuan(View view) {
        XinGuanActivity.startXinGuanActivity(this);
    }

    public void navigateToAskDoctor(View view) {
        AskDoctorActivity.startAskDoctorActivity(this);
    }

    public void navigateToMedicianShop(View view) {
        MedicianShopActivity.startMedicianShopActivity(this);
    }
}
